#include "PlayerCharacter.h"


PlayerCharacter::PlayerCharacter(Real id, String charType, SceneManager* sceneManager)
	: Character(id, charType, sceneManager)
	, mMoveDirection(Ogre::Vector3::ZERO)
{
	setupBody();
	setupCamera();
	setupAnimation();
}


PlayerCharacter::~PlayerCharacter(void)
{
}

void PlayerCharacter::injectKeyDown(const OIS::KeyEvent& evt)
{
	// keep track of the player's intended direction
	if (evt.key == OIS::KC_W) mMoveDirection.z = -1;
	else if (evt.key == OIS::KC_A) mMoveDirection.x = -1;
	else if (evt.key == OIS::KC_S) mMoveDirection.z = 1;
	else if (evt.key == OIS::KC_D) mMoveDirection.x = 1;

	/*if (!mMoveDirection.isZeroLength() && mBaseAnimID == ANIM_IDLE_BASE)
	{
		// start running if not already moving and the player wants to move
		setBaseAnimation(ANIM_RUN_BASE, true);
		if (mTopAnimID == ANIM_IDLE_TOP) setTopAnimation(ANIM_RUN_TOP, true);
	}*/
}

void PlayerCharacter::injectKeyUp(const OIS::KeyEvent& evt)
{
	// keep track of the player's intended direction
	if (evt.key == OIS::KC_W && mMoveDirection.z == -1) mMoveDirection.z = 0;
	else if (evt.key == OIS::KC_A && mMoveDirection.x == -1) mMoveDirection.x = 0;
	else if (evt.key == OIS::KC_S && mMoveDirection.z == 1) mMoveDirection.z = 0;
	else if (evt.key == OIS::KC_D && mMoveDirection.x == 1) mMoveDirection.x = 0;

	/*if (mMoveDirection.isZeroLength() && mBaseAnimID == ANIM_RUN_BASE)
	{
		// stop running if already moving and the player doesn't want to move
		setBaseAnimation(ANIM_IDLE_BASE);
		if (mTopAnimID == ANIM_RUN_TOP) setTopAnimation(ANIM_IDLE_TOP);
	}*/
}


void PlayerCharacter::setupBody()
{
	Character::setupBody();
}
void PlayerCharacter::setupCamera()
{
	mCam = mSceneMgr->getCamera("PlayerCam");
}
void PlayerCharacter::setupAnimation()
{
	Character::setupAnimation();
}
void PlayerCharacter::update(Real deltaTime)
{
	printf("Player");
	updateBody(deltaTime);
	updateCamera(deltaTime);
	updateAnimation(deltaTime);
	Character::update(deltaTime);
}


void PlayerCharacter::updateBody(Real deltaTime)
{

	Vector3 mGoalDirection = Vector3::ZERO;   // we will calculate this

	if (mMoveDirection != Vector3::ZERO)
	{
		// calculate actually goal direction in world based on player's key directions
		mGoalDirection.z += mMoveDirection.z;
		mGoalDirection.x += mMoveDirection.x;
		mGoalDirection.y = 0;
		mGoalDirection.normalise();

		Quaternion toGoal = mEntityNode->getOrientation().zAxis().getRotationTo(mGoalDirection);

		// calculate how much the character has to turn to face goal direction
		Real yawToGoal = toGoal.getYaw().valueDegrees();
		// this is how much the character CAN turn this frame
		//Real yawAtSpeed = yawToGoal / Math::Abs(yawToGoal) * deltaTime * TURN_SPEED;

		// turn as much as we can, but not more than we need to
		//if (yawToGoal < 0) yawToGoal = std::min<Real>(0, std::max<Real>(yawToGoal, yawAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, yawAtSpeed, 0);
		//else if (yawToGoal > 0) yawToGoal = std::max<Real>(0, std::min<Real>(yawToGoal, yawAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, 0, yawAtSpeed);
			
		mEntityNode->yaw(Degree(yawToGoal));

		// move in current body direction (not the goal direction)
		mEntityNode->translate(0, 0, deltaTime * RUN_SPEED, //* mAnims[mBaseAnimID]->getWeight(),
			Node::TS_LOCAL);
	}

	/*if (mBaseAnimID == ANIM_JUMP_LOOP)
	{
		// if we're jumping, add a vertical offset too, and apply gravity
		mBodyNode->translate(0, mVerticalVelocity * deltaTime, 0, Node::TS_LOCAL);
		mVerticalVelocity -= GRAVITY * deltaTime;
			
		Vector3 pos = mBodyNode->getPosition();
		if (pos.y <= CHAR_HEIGHT)
		{
			// if we've hit the ground, change to landing state
			pos.y = CHAR_HEIGHT;
			mBodyNode->setPosition(pos);
			setBaseAnimation(ANIM_JUMP_END, true);
			mTimer = 0;
		}
	}*/
	
}
 
void PlayerCharacter::updateCamera(Real deltaTime)
{
	mCam->setPosition(mEntityNode->getPosition().x,300,mEntityNode->getPosition().z + 300);
	mCam->lookAt(mEntityNode->getPosition());
}
void PlayerCharacter::updateAnimation(Real deltaTime)
{
	
}